import AppPage from "../../App";
import {call, Interval, Module, register, SagaGenerator} from "../core-native";
import {RootState} from "../types";
import AsyncStorage from "@react-native-async-storage/async-storage";
import {storages} from "../constants";

class AppModule extends Module<RootState, "app"> {
    *onEnter(): SagaGenerator {
        const userInfo = yield* call(() => AsyncStorage.getItem(storages.KEY_USERINFO));
        const token = yield* call(() => AsyncStorage.getItem(storages.KEY_TOKEN));
        if (userInfo && token) {
            this.setState({initialRouteName: "Tab"});
        } else {
            this.setState({initialRouteName: "Login"});
        }
    }
    *onAppActive(): SagaGenerator {
        console.log("App被激活");
    }
    *onAppInactive(): SagaGenerator {
        console.log("App进入后台");
    }
    @Interval(20)
    *onTick(): SagaGenerator {
        console.log("测试20s的Tick");
    }
}

const initState: RootState["app"]["app"] = {
    initialRouteName: "",
};

const module = register(new AppModule("app", initState));
export const AppActions = module.getActions();
export const App = module.attachLifecycle(AppPage);
